import 'package:flutter/material.dart';
import '../../helper/utils/util.dart';
import 'controller.dart';

class AppTabPage extends GetView<AppTabController> {
  const AppTabPage({Key? key}) : super(key: key);

  Widget getIcon(String url, {required int idx}) {
    var state = controller.state;
    if (idx == state.currentIndex.value && state.isRefreshing.value) {
      return LottieBuilder.asset(
        'assets/jsons/icon_refresh.json',
        width: 22.r,
        height: 22.r,
      );
    }
    return RedNumContainer(
      redCount: idx == 1 ? 5 : 0,
      childSize: 18.r,
      offx: 10.r,
      child: Image.asset(
        url.imgPathAppTab,
        width: 18.r,
        height: 18.r,
        fit: BoxFit.contain,
      ),
    );
  }

  // Widget getIcon(IconData icon, {bool isActive = false, required int idx}) {
  //   var state = controller.state;
  //   if (idx == state.currentIndex.value && state.isRefreshing.value) {
  //     return LottieBuilder.asset(
  //       'assets/jsons/icon_refresh.json',
  //       width: 22.r,
  //       height: 22.r,
  //     );
  //   }
  //   return Icon(
  //     icon,
  //     color: isActive ? ColorsUtil.theme : ColorsUtil.separator,
  //     size: 22.r,
  //   );
  // }

  @override
  Widget build(BuildContext context) {
    var state = controller.state;
    return Scaffold(
      body: PageView.builder(
        physics: const NeverScrollableScrollPhysics(),
        controller: state.pageController,
        itemBuilder: (context, index) {
          return controller.currPage(index);
        },
      ),
      bottomNavigationBar: Obx(() => BottomNavigationBar(
            type: BottomNavigationBarType.fixed,
            selectedItemColor: ColorsUtil.blue,
            unselectedItemColor: ColorsUtil.subTitle,
            items: controller.state.items
                .asMap()
                .map(
                  (key, e) => MapEntry(
                    key,
                    BottomNavigationBarItem(
                      label: e.title,
                      icon: getIcon(e.icon, idx: key),
                      activeIcon: GetBuilder<AppTabController>(
                        id: 'refresh',
                        builder: (controller) {
                          return getIcon(e.activeIcon, idx: key);
                        },
                      ),
                    ),
                  ),
                )
                .values
                .toList(),
            currentIndex: controller.state.currentIndex.value,
            selectedFontSize: 12.sp,
            unselectedFontSize: 12.sp,
            onTap: (index) => controller.clickTab(index),
            backgroundColor: Colors.white,
          )),
    );
  }
}
